/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.services;

import com.thienloc.omap.controller.AbstractCrudBean;
import com.thienloc.omap.controller.security.*;
import com.thienloc.omap.entity.ServiceConfig;
import com.thienloc.omap.entity.ServiceConfigSubCp;
import com.thienloc.omap.session.ServiceConfigFacade;
import com.thienloc.omap.session.ServiceConfigSubCpFacade;
import com.thienloc.omap.util.Constant;
import com.thienloc.omap.util.JsfUtil;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class SubCpConfigApproveBean extends AbstractCrudBean {

    @EJB
    private ServiceConfigSubCpFacade facade;
    @EJB
    private ServiceConfigFacade serviceConfigFacade;
    private List<ServiceConfigSubCp> list;
    private ServiceConfigSubCp[] selectedList;
    private Date startViewDate = createStartViewDate();
    private Date endViewDate = new Date();

    public void aprroveSelected() {
        for (ServiceConfigSubCp configSubCp : selectedList) {
            try {
                configSubCp.setApproveStatus("APPROVED");
                configSubCp.setApprovedBy(getUsername());
                configSubCp.setApprovedDate(new Date());
                facade.edit(configSubCp);

                ServiceConfig config = new ServiceConfig();
                config.setContent(configSubCp.getContent());
                config.setCpNumber(configSubCp.getCpNumber());
                config.setDeliverContent(configSubCp.getDeliverContent());
                config.setEndEffect(configSubCp.getEndEffect());
                config.setParam(configSubCp.getParam());
                config.setServiceName(configSubCp.getServiceName());
                config.setStartEffect(configSubCp.getStartEffect());
                serviceConfigFacade.create(config);
            } catch (Exception ex) {
                logAndNotifyError(ex);
            }
        }

        logger.info("Approved {} config(s) for subcp", selectedList.length);
        JsfUtil.addSuccessMessage("Success", "Approved selected config(s) for subcp");
    }

    @Override
    protected Logger getLogger() {
        return LoggerFactory.getLogger(ResourceBean.class);
    }

    @Override
    protected void resetToCreateFormAction() throws Exception {
        edit = false;
        view = false;

        endViewDate.setHours(23);
        endViewDate.setMinutes(59);
        endViewDate.setSeconds(59);

        list = facade.findInDateRange(startViewDate, endViewDate);
    }

    @Override
    protected void validateForm() throws Exception {
    }

    @Override
    protected void findSelected(int id) throws Exception {
        logAndNotifyError(new UnsupportedOperationException("Not Supported"));
    }

    @Override
    protected void createSelected() throws Exception {
        logAndNotifyError(new UnsupportedOperationException("Not Supported"));
    }

    @Override
    protected void editSelected() throws Exception {
        logAndNotifyError(new UnsupportedOperationException("Not Supported"));
    }

    @Override
    protected void removeSelected() throws Exception {
        logAndNotifyError(new UnsupportedOperationException("Not Supported"));
    }

    @Override
    protected void prepareViewAction() throws Exception {
        logAndNotifyError(new UnsupportedOperationException("Not Supported"));
    }

    @Override
    protected void prepareEditAction() throws Exception {
        logAndNotifyError(new UnsupportedOperationException("Not Supported"));
    }

    public List<ServiceConfigSubCp> getList() {
        return list;
    }

    public void setList(List<ServiceConfigSubCp> list) {
        this.list = list;
    }

    public ServiceConfigSubCp[] getSelectedList() {
        return selectedList;
    }

    public void setSelectedList(ServiceConfigSubCp[] selectedList) {
        this.selectedList = selectedList;
    }

    public Date getStartViewDate() {
        return startViewDate;
    }

    public void setStartViewDate(Date startViewDate) {
        this.startViewDate = startViewDate;
    }

    public Date getEndViewDate() {
        return endViewDate;
    }

    public void setEndViewDate(Date endViewDate) {
        this.endViewDate = endViewDate;
    }

    private Date createStartViewDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }
}
